//
//  FLWallpaperViewController.m
//  FeeLing
//
//  Created by 乔光玉 on 2019/1/2.
//  Copyright © 2019年 大仙儿飘. All rights reserved.
//
#import "PageSplitingManager.h"
#import "UIScrollView+MJRefresh.h"
#import "FLMineViewController.h"
#import "FLWallPaperDetailsViewController.h"
#import "FLWallpaperCollectionViewCell.h"
#import "FLWallpaperViewController.h"
#import "PHAssetModel.h"
#import <AVFoundation/AVAsset.h>
#import <AVFoundation/AVAssetImageGenerator.h>
#import <AVFoundation/AVTime.h>
#import <Photos/Photos.h>
@interface FLWallpaperViewController ()
<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    
   
    
}
@property(nonatomic,strong)UICollectionView * mainCollectionView;
@property(nonatomic,strong) AVQuery * livephotoQuery;
@property(nonatomic,strong)UIView * navView;
@property(nonatomic,strong)UILabel * navLabel;
@property(nonatomic,strong)UIButton * userButton;
@property (nonatomic, strong) NSMutableArray * pictureArr;
@property(nonatomic,strong)PageSplitingManager * pageManager;
@end

@implementation FLWallpaperViewController
-(void)viewDidAppear:(BOOL)animated{
    
   
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageManager = [[PageSplitingManager alloc] init];
    self.pageManager.pageSize = 20;
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#2B2D5C"];
    self.navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 64)];
    self.navView.backgroundColor = [UIColor colorWithHexString:@"32346A"];
    [self.view addSubview:self.navView];
    
    self.navLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, SCREENWIDTH, 25)];
    self.navLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size: 18];
    self.navLabel.textAlignment = NSTextAlignmentCenter;
    self.navLabel.textColor = [UIColor whiteColor];
    self.navLabel.text = NSLocalizedString(@"wallpaper", nil);
    [self.view addSubview:self.navLabel];
    
    self.userButton = [UIButton new];
    self.userButton.frame = CGRectMake(SCREENWIDTH-12-22, 32, 22, 22);
    [self.userButton setImage:[UIImage imageNamed:@"我的"] forState:UIControlStateNormal];
    [self.userButton addTarget:self action:@selector(userButtonclick:) forControlEvents:UIControlEventTouchUpInside];
    //[self.view addSubview:self.userButton];
    
    _pictureArr = [[NSMutableArray alloc]init];
    
    //1.初始化layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //设置collectionView滚动方向
    //    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    //设置headerView的尺寸大小
    //layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 100);
    //该方法也可以设置itemSize
    CGFloat with = (SCREENWIDTH-60)/3 ;
    
    layout.itemSize =CGSizeMake(with, with/105*108);
    
    //2.初始化collectionView
    _mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 64, SCREENWIDTH-20, SCREENHEIGHT-64) collectionViewLayout:layout];
    if (SCREENHEIGHT>800) {
        
        self.navView.frame =CGRectMake(0, 0, SCREENWIDTH, 84);
        self.navLabel.frame =CGRectMake(0, 30+20, SCREENWIDTH, 25);
        _mainCollectionView.frame =CGRectMake(10, 84, SCREENWIDTH-20, SCREENHEIGHT-84);
    }
    [self.view addSubview:_mainCollectionView];
    _mainCollectionView.backgroundColor = [UIColor colorWithHexString:@"#2B2D5C"];
    
    //3.注册collectionViewCell
    //注意，此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
    [_mainCollectionView registerClass:[FLWallpaperCollectionViewCell class] forCellWithReuseIdentifier:@"FLWallpaperCollectionViewCell"];
    
    //注册headerView  此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致  均为reusableView
    [_mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];
    
    //4.设置代理
    _mainCollectionView.delegate = self;
    _mainCollectionView.dataSource = self;
    
    [_mainCollectionView reloadData];
    _mainCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    _mainCollectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    
    [self loadData];
}

-(void)loadMore {
    self->_livephotoQuery.skip = [_pageManager getNextStartAt];
    [self->_livephotoQuery findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
        [self.mainCollectionView.mj_footer endRefreshing];
        [self->_pageManager pageLoadCompleted];
        [self.pictureArr addObjectsFromArray:objects];
        [self.mainCollectionView reloadData];
    }];
}
//loadData方法

-(void)loadData{
    
    [self->_pageManager resetPageSplitingManager];
    self->_livephotoQuery = [AVQuery queryWithClassName:@"WallPaper"];
    [_livephotoQuery orderByDescending:@"browseNum"];
    self->_livephotoQuery.limit = 20;
    [self->_livephotoQuery findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
        [self.mainCollectionView.mj_header endRefreshing];
        [self->_pageManager pageLoadCompleted];
        self.pictureArr = [NSMutableArray arrayWithArray:objects];
        [self.mainCollectionView reloadData];
    }];
    
    
}
#pragma mark collectionView代理方法
//返回section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.pictureArr.count;
}





- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    FLWallpaperCollectionViewCell *cell = (FLWallpaperCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"FLWallpaperCollectionViewCell" forIndexPath:indexPath];
    
    
    NSURL * imageurl =[NSURL URLWithString:self.pictureArr[indexPath.row][@"imageFileUrl"]];
    
    
    [cell.topImage sd_setImageWithURL:imageurl];
    
    return cell;
}

//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat with = (SCREENWIDTH-60)/3 ;
    CGFloat hiegh =SCREENHEIGHT/SCREENWIDTH*with;
    
    return CGSizeMake(with, hiegh);
}


//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}


//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath


{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"bizhipush"object:@(0)];
    
    
    self.hidesBottomBarWhenPushed=YES;
    
    FLWallPaperDetailsViewController * vc =[[FLWallPaperDetailsViewController alloc]init];
    vc.object = self.pictureArr[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
     self.hidesBottomBarWhenPushed=NO;
    
}
-(void)userButtonclick:(UIButton *)sender{
    self.hidesBottomBarWhenPushed=YES;
    FLMineViewController * vc =[[FLMineViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    self.hidesBottomBarWhenPushed=NO;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
